package com.bauer.base.thread;

/**
 * 测试java死锁情况  可以同jstack 命令查看
 */
public class Deadlock {

    static class A {

    }

    static class B{

    }

    public static void main(String[] args) {
        deadlock1();
    }

    public static void deadlock1(){
        final A a = new A();
        final B b = new B();
        Thread thread1 =  new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (a){
                    try {
                        /**
                         * 触发等待   使得死锁必现   否则可能出现线程1 执行完了才执行线程2
                         */
                        Thread.sleep(100);
                        synchronized (b){

                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread thread2 =  new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (b){
                    synchronized (a){
                        System.out.println("thread2 get a");
                    }
                }
            }
        });

        thread1.start();
        thread2.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread1.interrupt();
        thread1.interrupt();
        System.out.println("ss");
    }
}
